'''
    Regular expressions module to hold all useful IRC message patterns.
'''

import re

pieces = { 'nick' : r'(?P<nick>[\[\]\\|a-zA-Z]\S*?)',
           'username' : '(?P<username>\S+?)',
           'hostname' : '(?P<hostname>\S+?)',
           'channel' : '(?<=[ :])(?P<channel>[#&]\S+?)',
           #Use this when interacting with just the server
           'chan_greedy': '(?<=[: ])(?P<channel>[&#]\S+)',
           'msg' : '(?P<message>.*)'
         }

pieces['hoststr'] = '(?P<hoststr>%(nick)s!%(username)s@%(hostname)s)' % pieces

privmsg = re.compile(':%(hoststr)s PRIVMSG (?P<channel>(?:[\[\]\\|a-zA-Z]\S*?)|(?:[&#!]\S+)) :%(msg)s' % pieces)
notice = re.compile(':%(hoststr)s NOTICE (?:%(channel)s|(?P<ownnick>[\[\]\\|a-zA-Z]\S*?)) :%(msg)s' % pieces)
mode = re.compile(':(%(hoststr)s|(?P<modesetter>\S+)) MODE (%(channel)s|(?P<ownnick>\S+)) :?(?P<mode>.*)' % pieces)
nick = re.compile(':%(hoststr)s NICK :(?P<newnick>\S+)' % pieces)
topic = re.compile(':%(hoststr)s TOPIC %(channel)s :(?P<topic>.*)' % pieces)
topic_notice = re.compile(':(?P<server>\S+) 332 (?P<ownnick>\S+) %(channel)s :(?P<topic>.*)' % pieces)
kick = re.compile(':%(hoststr)s KICK %(channel)s :(?P<kickee>.*)' % pieces)
ping = re.compile('PING :(?P<servername>.+)')
part = re.compile(':%(hoststr)s PART %(chan_greedy)s(?: (?P<reason>.*))?' % pieces)
quit = re.compile(':%(hoststr)s QUIT :(?P<reason>.*)' % pieces)
join = re.compile(':%(hoststr)s JOIN :%(chan_greedy)s' % pieces)
listing = re.compile(':(?P<server>\S+) (?P<code>3(?:(?:53)|(?:66))) (?P<ownnick>\S+) (?:. )?%(chan_greedy)s :(?P<names>.*)' % pieces)

guess_channel = re.compile('%(chan_greedy)s' % pieces)
